<?php

namespace app\models\distribution;

use Yii;

/**
 * This is the model class for table "distribution_group".
 *
 * @property int $id
 * @property float $order_amount 成为团长,下单金额(拼团区)
 * @property float $commission 团长分享团长,得多少佣金
 * @property int $cash_num 分享多少可以提现
 * @property float $cash_amount 佣金满多少可以提现
 */
class DistributionGroup extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'distribution_group';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['order_amount', 'commission', 'cash_amount'], 'number'],
            [['commission'], 'required'],
            [['cash_num'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_amount' => 'Order Amount',
            'commission' => 'Commission',
            'cash_num' => 'Cash Num',
            'cash_amount' => 'Cash Amount',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['DistributionGroup' => $data]) && $model->save()) {
            return true;
        }
    }
}
